package com.stephen.jsbridge.utils;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Stephen on 2016/3/3.
 * 动态注入不影响原布局的无内容提示界面
 */
public class StephenCommonNoDataView implements Serializable {
    private Activity activity;
    private View mainContentView,finalCreateView;
    private RelativeLayout mainNoDataRy;
    private TextView centerTV;
    private boolean isResponseClick = true,isHintImgHide = false;
    private List<Integer> bottomBtnIds;
    private Drawable imgDrawable;
    private int imgResId = -1,imgWidthDp,imgHeightDp,imgPaddingDp;

    public StephenCommonNoDataView(Activity activity) {
        this.activity = activity;
    }

    public View initAndInjectNoDataViewForAllViewReturnView(int mainLayoutId, int replaceViewId) {//注入全部,返回view
        View mainView = LayoutInflater.from(activity).inflate(mainLayoutId,null);
        if(null != mainView)return initAndInjectNoDataViewForAllView(mainView.findViewById(replaceViewId));
        return null;
    }

    public void initAndInjectNoDataViewForAllViewWithActivity(int mainLayoutId,int replaceViewId){//注入全部,直接用于activity
        View mainView = LayoutInflater.from(activity).inflate(mainLayoutId,null);
        if(null == mainView){
            activity.setContentView(mainLayoutId);
        }else{
            activity.setContentView(initAndInjectNoDataViewForAllView(mainView.findViewById(replaceViewId)));
        }
    }

    public View initAndInjectNoDataViewForPartViewReturnView(int mainLayoutId, int replaceViewId, ViewGroup.LayoutParams layoutParams) {//注入部分,返回view
        View mainView = LayoutInflater.from(activity).inflate(mainLayoutId,null);
        if(null != mainView)return initAndInjectNoDataViewForPartView(mainView.findViewById(replaceViewId),mainView,layoutParams);
        return null;
    }

    public void initAndInjectNoDataViewForPartViewWithActivity(int mainLayoutId,int replaceViewId,ViewGroup.LayoutParams layoutParams){//注入部分,直接用于activity
        View mainView = LayoutInflater.from(activity).inflate(mainLayoutId,null);
        if(null == mainView){
            activity.setContentView(mainLayoutId);
        }else{
            activity.setContentView(initAndInjectNoDataViewForPartView(mainView.findViewById(replaceViewId),mainView,layoutParams));
        }
    }

    public View initAndInjectNoDataViewForAllView(View mainContentView){//注入全部,可直接用于fragment
        finalCreateView = initInjectSpecificView(mainContentView,null);
        return finalCreateView;
    }

    public View initAndInjectNoDataViewForPartView(View replaceView, View mainView, ViewGroup.LayoutParams layoutParams){//注入部分,可直接用于fragment
        ViewGroup rootView = StephenToolUtils.convertRootViewToViewGroup(mainView);
        int customerLvIndex = StephenToolUtils.getChildViewInRootViewIndex(rootView,mainView,replaceView.getId());
        if(-1 != customerLvIndex)rootView.addView(initInjectSpecificView(replaceView,rootView),customerLvIndex,layoutParams);
        //finalCreateView = mainView;
        finalCreateView = rootView;
        return finalCreateView;
    }

    private View initInjectSpecificView(View mainContentView, ViewGroup rootView){
        FrameLayout mainContainerFy = new FrameLayout(activity);//主容器

        this.mainContentView = mainContentView;
        if(null != rootView)rootView.removeView(mainContentView);//rootView主要用于只替换布局中部分的显示内容,如果有必须在下步之前移除
        mainContainerFy.addView(mainContentView,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        //create no data view
        mainNoDataRy = new RelativeLayout(activity);
        mainNoDataRy.setBackgroundColor(Color.parseColor("#f7f7f8"));
        centerTV = new TextView(activity);
        centerTV.setId(StephenToolUtils.generateViewId());
        centerTV.setText("没有数据,孤独寂寞冷,重新加载试试...");
        centerTV.setGravity(Gravity.CENTER);
        centerTV.setTextSize(15);//sp
        centerTV.setTextColor(Color.parseColor("#646464"));
        centerTV.setSingleLine(false);
        RelativeLayout.LayoutParams centerTvLp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        centerTvLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        mainNoDataRy.addView(centerTV,centerTvLp);
        mainContainerFy.addView(mainNoDataRy,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        setNoDataViewHide();//默认隐藏
        return mainContainerFy;
    }

    //更改提示文本
    public void setCenterTextViewStr(String hintStr){
        if(null != centerTV)centerTV.setText(hintStr);
    }

    //提示文本上面的提示图
    public void setCenterTextTopHintImg(int imgResId,int imgWidthDp,int imgHeightDp,int imgPaddingDp){
        isHintImgHide = false;
        this.imgResId = imgResId;
        this.imgWidthDp = imgWidthDp;
        this.imgHeightDp = imgHeightDp;
        this.imgPaddingDp = imgPaddingDp;
        StephenToolUtils.setTextViewAroundDrawable(activity,centerTV,imgResId,imgWidthDp,imgHeightDp,imgPaddingDp, Gravity.TOP);
    }

    public void setCenterTextTopHintImg(Drawable imgDrawable, int imgWidthDp, int imgHeightDp, int imgPaddingDp){
        isHintImgHide = false;
        this.imgDrawable = imgDrawable;
        this.imgWidthDp = imgWidthDp;
        this.imgHeightDp = imgHeightDp;
        this.imgPaddingDp = imgPaddingDp;
        StephenToolUtils.setTextViewAroundDrawable(activity,centerTV,imgDrawable,imgWidthDp,imgHeightDp,imgPaddingDp, Gravity.TOP);
    }

    public void setCenterTextTopHintImgHide(){
        isHintImgHide = true;
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(Color.TRANSPARENT);
        StephenToolUtils.setTextViewAroundDrawable(activity,centerTV,gradientDrawable,0,0,0, Gravity.TOP);
    }

    //提示文本和提示图一起设置
    public void setCenterTextStrAndHintImg(String hintStr, int imgResId, int imgWidthDp, int imgHeightDp, int imgPaddingDp){
        setCenterTextViewStr(hintStr);
        setCenterTextTopHintImg(imgResId,imgWidthDp,imgHeightDp,imgPaddingDp);
    }

    //提示文本下面的按钮,支持多次设置
    public void setCenterTextBottomBtn(String btnText, int btnColor, Drawable btnBgSelector, int imgWidthDp, int imgHeightDp, int imgMarginDp, View.OnClickListener onClickListener){
        if(null != mainNoDataRy){
            Button bottomBtn = new Button(activity);
            bottomBtn.setId(StephenToolUtils.generateViewId());
            bottomBtn.setGravity(Gravity.CENTER);
            bottomBtn.setText(btnText);
            bottomBtn.setTextColor(btnColor);
            bottomBtn.setSingleLine(true);
            StephenToolUtils.setBackgroundAllVersion(bottomBtn,btnBgSelector);
            RelativeLayout.LayoutParams bottomBtnLp = new RelativeLayout.LayoutParams(StephenToolUtils.dip2px(activity,imgWidthDp), StephenToolUtils.dip2px(activity,imgHeightDp));
            bottomBtnLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
            bottomBtnLp.setMargins(0,StephenToolUtils.dip2px(activity,imgMarginDp),0,0);
            if(null == bottomBtnIds){
                if(null != centerTV)bottomBtnLp.addRule(RelativeLayout.BELOW,centerTV.getId());
                bottomBtnIds = new ArrayList<Integer>();
            }else{
                bottomBtnLp.addRule(RelativeLayout.BELOW,bottomBtnIds.get(bottomBtnIds.size()-1));
            }
            bottomBtnIds.add(bottomBtn.getId());
            mainNoDataRy.addView(bottomBtn,bottomBtnLp);
            bottomBtn.setOnClickListener(onClickListener);
        }//end of if
    }

    public void setNoDataViewShow(){
        setNoDataViewShow(true,"没有数据,孤独寂寞冷,重新加载试试...");
    }

    public void setNoDataViewShow(String hintStr){
        setNoDataViewShow(true, hintStr);
    }

    public void setNoDataViewShow(boolean isResponseClick,String hintStr){
        this.isResponseClick = isResponseClick;
        setCenterTextViewStr(hintStr);
        if(isHintImgHide){
            if(-1 != imgResId){
                setCenterTextTopHintImg(imgResId,imgWidthDp,imgHeightDp,imgPaddingDp);
            }else if(null != imgDrawable){
                setCenterTextTopHintImg(imgDrawable,imgWidthDp,imgHeightDp,imgPaddingDp);
            }//end of if
        }//end of if
        if(null != mainContentView)if(mainContentView.getVisibility() != View.GONE)mainContentView.setVisibility(View.GONE);
        if(null != mainNoDataRy)if(mainNoDataRy.getVisibility() != View.VISIBLE)mainNoDataRy.setVisibility(View.VISIBLE);
    }

    public void setNoDataViewHide(){
        setResponseClick(true);
        if(null != mainNoDataRy)if(mainNoDataRy.getVisibility() != View.GONE)mainNoDataRy.setVisibility(View.GONE);
        if(null != mainContentView)if(mainContentView.getVisibility() != View.VISIBLE)mainContentView.setVisibility(View.VISIBLE);
    }

    public void setResponseClick(boolean responseClick) {
        isResponseClick = responseClick;
    }

    public boolean isResponseClick() {
        return isResponseClick;
    }

    public View getFinalCreateView(){
        return finalCreateView;
    }

    public void setNoDataViewClickListener(View.OnClickListener onClickListener){
        if(null != mainNoDataRy)mainNoDataRy.setOnClickListener(onClickListener);
    }
}